#include"BlockQueue.hpp"
#include"Task.hpp"

string opers = "+-*/%";

void *Consumer(void* args)
{
    BlockQueue<Task> *bq = static_cast<BlockQueue<Task>*>(args);
    while(true)
    {
        Task t = bq->Pop();
        // usleep(10000);
        // cout<<"购入了一个商品"<<endl;
        t.run();
        cout<<"处理任务中  "<<"运算结果为"<<t.GetResult();
    }
    
}

void *Productor(void* args)
{
    int len = opers.size();
    BlockQueue<Task> *bq = static_cast<BlockQueue<Task>*>(args);
    // int products = 0;
    while(true)
    {
        int data1 = rand() % 10 + 1;
        int data2 = rand() % 10 + 1;

        usleep(10);
        char op = opers[len];
        Task t(data1, data2, op);

        bq->Push(t);
        // products++;
        usleep(1000);
        cout<<"生产了一个商品"<<endl;
    }
}



int main()
{
    //BlockQueue 传对象更为形象
    BlockQueue<int> *bq = new BlockQueue<int>();

    pthread_t c,p;
    pthread_create(&c, nullptr, Consumer, bq);
    pthread_create(&p, nullptr, Productor, bq);


    pthread_join(c, nullptr);
    pthread_join(p, nullptr);

    delete bq;
    return 0;
}